package com.wanpinghui.supplier.common;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.wanpinghui.wphlibrary.webapp.WPHWebView;
import com.yunshl.yunshllibrary.utils.LogUtils;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2018/1/15
 ***/

public abstract class BaseWebActivity extends BaseActivity {
    private boolean isNoError = true;

    @Override
    public void onBackPressed() {
        if (getWebView().canGoBack()) {
            getWebView().goBack();
        } else {
            finish();
        }
    }

    protected abstract WPHWebView getWebView();

    @Override
    public void initData() {
        if (getWebView() != null) {
            getWebView().setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    LogUtils.w("BaseWebActivity", "webview =====>" + "onPageFinished() url : " + url);
                    if (isNoError) {
                        onLoadSuccess();
                    }
                    super.onPageFinished(view, url);
                }

                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                    isNoError = true;
                    onLoadStart();
                    LogUtils.w("BaseWebActivity", "webview =====>" + "onPageStarted() url : " + url);
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    LogUtils.w("BaseWebActivity", "webview =====>" + "shouldOverrideUrlLoading() url : " + url);
                    view.loadUrl(url);
                    return true;
                }


                @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    //返回false，意味着请求过程里，不管有多少次的跳转请求（即新的请求地址），均交给webView自己处理，这也是此方法的默认处理
                    //返回true，说明你自己想根据url，做新的跳转，比如在判断url符合条件的情况下，我想让webView加载http://ask.csdn.net/questions/178242
                    try {
                        view.loadUrl(request.getUrl().toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return true;
                }


                @Override
                public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                    handler.proceed();
                    isNoError = false;
                    onLoadError();
                    super.onReceivedSslError(view, handler, error);
                }

                @Override
                public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
                    isNoError = false;
                    onLoadError();
                    super.onReceivedHttpError(view, request, errorResponse);
                }

                @Override
                public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                    isNoError = false;
                    onLoadError();
                    super.onReceivedError(view, request, error);
                }

                @Override
                public void onLoadResource(WebView view, String url) {
                    super.onLoadResource(view, url);
                }
            });
        }
    }

    protected abstract void onLoadError();

    protected abstract void onLoadSuccess();

    protected abstract void onLoadStart();
}
